﻿/*
 * marqdown
 * --------
 * a C# Markdown processor
 * 
 * Copyright (c) 2010 Andrew Wall
 * http://code.google.com/p/marqdown/
 * 
 * 
 */


#region Copyright and license

/*

Copyright (c) 2010 Andrew Wall

 http://www.opensource.org/licenses/mit-license.php
  
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#endregion


using System;

namespace Marqdown
{
    namespace PostProcessing
    {
        public abstract class LinkCommon : CommonState
        {
            protected virtual void appendHref(PostProcessor info)
            {
                var link = info.field1.ToString();
                var item = getURL(info, link);
                info.Append(buildHref(item, link));
            }
            protected LinkItem getURL(PostProcessor info, string link)
            {
                string tmp = link.Replace('\n', ' ');
                return info.getLink(tmp);
            }
            protected string buildAttribute(string name, string value)
            {
                if ( (name.Length>0) && value.Length>0)
                {
                    return " "+name+"=\""+value+"\"";
                }
                else
                {
                    return "";
                }
            }
            protected string buildHref(LinkItem item, string text)
            {
                return "<a" + buildAttribute("href", item.url)+buildAttribute("title",item.title) + ">" + text + "</a>";
            }
        }
        public class Link : LinkCommon
        {
            internal static CommonState linkAbsorbSpace = new LinkAbsorbSpace();
            internal static CommonState linkOpen = new LinkOpen();
            internal static CommonState linkKey = new LinkKey();
            public override void process(PostProcessor info, char c)
            {
                switch (c)
                {
                    case ']':
                        // appendHref(info);
                        // info.ReturnToPlain();
                        info.ChangeState(linkAbsorbSpace);
                        break;
                    default:
                        info.field1.Append(c);
                        break;
                }
            }

            public override bool newLine(PostProcessor info) {
                info.field1.Append('\n');
                return false;
            }
        }
        public class LinkAbsorbSpace : LinkCommon
        {
            public override void process(PostProcessor info, char c)
            {
                switch (c)
                {
                    case ' ':
                        info.ChangeState(Link.linkOpen);
                        break;
                    case '[':
                        info.ChangeState(Link.linkKey);
                        break;
                    default:
                        appendHref(info);
                        info.Append(c);
                        info.ReturnToPlain();
                        break;
                }
            }
        }
        public class LinkOpen : LinkCommon
        {
            public override void process(PostProcessor info, char c)
            {
                switch (c)
                {
                    case '[':
                        info.ChangeState(Link.linkKey);
                        break;
                    default:
                        appendHref(info);
                        info.Append(c);
                        info.ReturnToPlain();
                        break;
                }
            }
        }
        public class LinkKey : LinkCommon
        {
            public override void process(PostProcessor info, char c)
            {
                switch (c)
                {
                    case ']':
                        appendHref(info); // separate key and text
                        info.ReturnToPlain();
                        break;
                    default:
                        info.field2.Append(c);
                        break;
                }
            }
            protected override void appendHref(PostProcessor info)
            {
                var text = info.field1.ToString();
                var link = allowForEmptyKey(info.field2.ToString(), text);
                info.Append(buildHref(getURL(info, link), text));
            }
            private string allowForEmptyKey(string link, string text)
            {
                string key = link;
                if (key.Length == 0)
                {
                    key = text;
                }
                return key;
            }
        }

    }
}